//
//  ViewController.m
//  TestAFN
//
//  Created by China Dragon on 2023/8/26.
//

#import "ViewController.h"

//引用项目级的头文件使用 <>
#import <AFNetworking/AFNetworking.h>
//SDWebImage
#import <SDWebImage/SDWebImage.h>

#import "NetworkTools.h"

#import <WebKit/WebKit.h>

/*
 
 IOS之AFN框架的使用(文件下载和上传)
 https://www.freesion.com/article/98431335668/
 
 AFN常见网络错误
 1、不支持的内容 Status Code: 200 ， 需要修改反序列化，例如：@"text/html"
 2、Status Code: 405 Request failed: method not allowed (405)
    对应的Url 不支持 该请求方式
 
 */

@interface ViewController ()
@property (nonatomic, weak) UIImageView *imageview;

@property (nonatomic, weak)WKWebView *webview;
//@property (nonatomic, weak)UIWebView *webview;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    [self loadImage];
    
//    [self loadNetData1];
//    [self loadNetData2];
    
    [self loadwebview];
   
}

/*
 
 2020 年 4 月起App Store将不再接受使用UIWebView的新App， 2020 年 12 月起将不再接受使用UIWebView的App更新。
 
 iOS学习笔记 — WKWebView的使用
 https://blog.csdn.net/Alexander_Wei/article/details/78360591
 
 iOS13 WKWebView使用与UIWebView的差异和坑
 https://www2.jianshu.com/p/7943f352d248
 */
-(void) loadwebview{
    self.webview = [[WKWebView alloc] init];
//    self.webview = [[UIWebView alloc] init];
    self.webview.frame = self.view.bounds;
    [self.view addSubview:self.webview];
    
//    NSURL *url = [NSURL URLWithString:@"https://www.baidu.com/"];
    NSURL *url = [NSURL URLWithString:@"https://m.baidu.com/"];
//    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    // 设置请求头， - forHTTPHeaderField 所有告诉服务器 额外信息，都是通过此方法
    // 告诉服务器，我是iPhone
    [request setValue: @"iPhone" forHTTPHeaderField: @"User-Agent"];// 这样可以让加载的页面好看些
    
    [[[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                //将 NSData -> html 字符串
                NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                 //用weibview加载html
                //baseURL 是加载素材的网页服务器路径
                [self.webview loadHTMLString:html baseURL: url];
                //baseURL: nil 页面的图片没有显示出来
//                [self.webview loadHTMLString:html baseURL: nil];
                
    }]resume];
    
}

-(void) loadNetData2{
//    [[NetworkTools sharedTools] request:@"weather1d/101020100.shtml" parameters:nil];
//    [[NetworkTools sharedTools] request:@"mweather/101020300.shtml" parameters:nil];
    
//    [[NetworkTools sharedTools] request:@"api/weather/city/101030100" parameters:nil finished:^(id  _Nonnull result, NSError * _Nonnull error) {
//        if(error != nil){
//            NSLog(@" 请求失败 -> %@",error);
//            return;
//        }
//
//        NSLog(@" 请求成功 -> %@",result);
//    }];
    
    
    
    [[NetworkTools sharedTools] request: POST URLString:@"post" parameters:@{@"name": @"test", @"age": @18} finished:^(id  _Nonnull result, NSError * _Nonnull error) {
        if(error != nil){
            NSLog(@" 请求失败 -> %@",error);
            return;
        }
        
        if(result == nil){
            NSLog(@" 请求成功 -> result 为 nil");
            return;
        }
        
        NSLog(@" 请求成功 -> %@",result);
    }];
}

-(void) loadImage {
    UIImageView *iv = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 200, 130)];
    [self.view addSubview: iv];
    self.imageview = iv;
    NSURL *imgUrl = [NSURL URLWithString: @"https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"];
//    NSURL *imgUrl = @"https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png";//这里会有警告提示
    [self.imageview sd_setImageWithURL: imgUrl];
}

-(void)loadNetData1 {
    NSString *url = @"http://t.weather.itboy.net/api/weather/city/101030100";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    /*
     设置反序列化格式
     查看 AFJSONResponseSerializer 源码 找到 AFJSONResponseSerializer init
     默认格式 [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", nil];
     */
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript" , @"text/html", nil];
    
    //   [manager GET:url parameters:nil headers:(nullable NSDictionary<NSString *,NSString *> *) progress:^(NSProgress * _Nonnull
    
//    [manager GET:url parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
//
//    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSLog(@" success -> %@",responseObject);
//    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
//        NSLog(@" failure -> %@",error);
//    }];
    
    [manager GET:url parameters:nil headers:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@" success -> %@",responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@" failure -> %@",error);
    }];
}


@end
